// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import {Role} from './Role';
import {Weapon} from './Weapon/Weapon';

@ccclass
export class Player extends Role 
{
    @property(Weapon)
    weapon : Weapon = null;

    isMoveLeft = false;
    isMoveRight = false;
    jumpHeight = 1.5;
    moveSpeed = 2;
    gravity = 10;
    jumpCount = 0;
    maxJumpCount = 2;  
        
    isAttack = false;

    // LIFE-CYCLE CALLBACKS:

    onLoad () 
    {
        this.animation.on('finished', this.OnFinished, this)
        this.rigidBody.enabledContactListener = true;

        this.weapon = this.node.getComponentInChildren(Weapon);        
    }

    start () {

    }

    DoJump()
    {
        if(this.isAttack)
        return;
        
        if(this.rigidBody.linearVelocity.y < 0 && this.jumpCount < this.maxJumpCount)
        {
            ++this.jumpCount;
            var y = Math.sqrt(2 * this.gravity * this.jumpHeight);
            cc.log(y);
            var speed = this.rigidBody.linearVelocity;
            speed.y = y * 64;
    
            this.rigidBody.linearVelocity = speed;
        }
        else if(this.rigidBody.linearVelocity.y == 0)
        {
            this.jumpCount = 1;
            var y = Math.sqrt(2 * this.gravity * this.jumpHeight);
            cc.log(y);
            var speed = this.rigidBody.linearVelocity;
            speed.y = y * 64;
    
            this.rigidBody.linearVelocity = speed;
        }
    }

    DoAttack()
    {
        if(this.isAttack)
            return;
        
        this.animation.play("attack");
        this.isAttack = true;
    }

    update (dt) 
    {
        if(this.rigidBody.linearVelocity.y == 0)
        {
            this.jumpCount = 0;
        }

        if(this.isAttack)
        return;
        if(this.isMoveLeft || this.isMoveRight)
        {
            var speed = cc.Vec2.ZERO;
            if(this.isMoveLeft)
            {
                this.node.scaleX = -1;
                speed.x = -this.moveSpeed * 64;
            }
            else if(this.isMoveRight)
            {
                this.node.scaleX = 1;
                speed.x = this.moveSpeed * 64;
            }
            
            speed.y = this.rigidBody.linearVelocity.y;
            this.rigidBody.linearVelocity = speed;
        }
    }    

    onCollisionEnter (other:cc.Collider, self:cc.Collider) 
    {
        if(other.tag == 10)
        {
            cc.director.loadScene('helloworld');
        }
    }

    OnFinished(event)
    {      
        var state : cc.AnimationState = event.detail;          
        cc.log(state.name);
        if(state.name == 'attack')
        {
            this.isAttack = false;
            this.animation.play('idle');
        }        
    }

    onBeginContact (contact:cc.PhysicsContact, selfCollider:cc.PhysicsCollider, otherCollider:cc.PhysicsCollider) : void
    {
        if(otherCollider.tag  == 10)
        {
            cc.director.loadScene('helloworld');
        }
    }
}
